package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Negocio2.*;

public class DisciplinaDAO {
	private Connection conexao;
	public DisciplinaDAO() throws SQLException{
		this.conexao = CriaConexao.getConexao();
	}
	public void adiciona(Disciplina disciplina)throws SQLException{
		
		//Prepara conex�o
		String sql = "insert into disciplinas(nome,codigo)"+
		"values(?,?)";
		PreparedStatement stmt = conexao.prepareStatement(sql);
		
		
		//Seta os valores
		stmt.setString(1, disciplina.getNome());
		stmt.setString(2, disciplina.getCodigo());
		
		
		//Executa o c�digo SQL
		stmt.execute();
		stmt.close();
	}
	public List<Disciplina> getLista() throws SQLException{
		String sql = "select * from disciplinas";
		PreparedStatement stmt = this.conexao.prepareStatement(sql);
		ResultSet rs = stmt.executeQuery();
		
		List<Disciplina> listaDisciplinas = new ArrayList<Disciplina>();
		
		while(rs.next()){
			Disciplina disciplina = new Disciplina();
			disciplina.setNome(rs.getString("nome"));
			disciplina.setCodigo(rs.getString("codigo"));
			listaDisciplinas.add(disciplina);
		}
		rs.close();
		stmt.close();
		return listaDisciplinas;
	}
	
	public void altera(Disciplina disciplina) throws SQLException{
		String sql = "update disciplinas set nome=?, codigo=? where codigo=?";
		PreparedStatement stmt = conexao.prepareStatement(sql);	
		
		//Seta os valores
		stmt.setString(1, disciplina.getNome());
		stmt.setString(2, disciplina.getCodigo());
		stmt.setString(3, disciplina.getCodigo());

		
		//Executa o c�digo SQL
		stmt.execute();
		stmt.close();
	}
	
	public void remove(Disciplina disciplina) throws SQLException{
		String sql = "delete from disciplinas where cod_disciplina =?";
		PreparedStatement stmt = conexao.prepareStatement(sql);
		stmt.setString(1, disciplina.getCodigo());
		stmt.execute();
		stmt.close();
	}
}

